package com.knowvoyage.knowvoyageserver.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.knowvoyage.knowvoyageserver.pojo.dto.LearnDTO;
import com.knowvoyage.knowvoyageserver.pojo.entity.Learn;
import com.knowvoyage.knowvoyageserver.pojo.vo.Result;
import com.knowvoyage.knowvoyageserver.service.LearnService;
import com.knowvoyage.knowvoyageserver.util.JwtUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Tag(name = "学习记录类", description = "学习记录相关接口")
@Slf4j
@RestController
@RequestMapping("/learn")
@RequiredArgsConstructor
public class LearnController {
    private final LearnService learnService;
    private final JwtUtil jwtUtil;

    @Operation(summary = "插入学习记录", description = "插入学习记录接口")
    @PostMapping("/insert")
    public Result<Boolean> insert(@RequestBody Learn learn) {
        return learnService.insert(learn);
    }

    @Operation(summary = "根据ID查询", description = "根据ID查询接口")
    @GetMapping("/selectById")
    public Result<Learn> selectById(@RequestParam Long learnId) {
        return learnService.selectById(learnId);
    }

    @Operation(summary = "更新信息", description = "更新信息接口")
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody Learn learn) {
        return learnService.update(learn);
    }

    @Operation(summary = "删除学习记录", description = "删除学习记录接口")
    @GetMapping("/delete")
    public Result<Boolean> delete(@RequestParam Long learnId) {
        return learnService.delete(learnId);
    }

    @Operation(summary = "根据用户ID获取所有用户列表", description = "根据用户ID获取所有用户列表接口")
    @GetMapping("/selectByUserId")
    public Result<List<Learn>> selectByUserId(@RequestParam Long userId) {
        return learnService.selectByUserId(userId);
    }

    @Operation(summary = "获取所有用户列表", description = "获取所有用户列表接口")
    @GetMapping("/selectAll")
    public Result<List<Learn>> selectAll() {
        return learnService.selectAll();
    }

    @Operation(summary = "前端插入学习记录", description = "可插入一条学习记录")
    @PostMapping("/record")
    public Result<Boolean> recordLearning(
            @RequestBody LearnDTO learnDTO,
            @RequestHeader("Authorization") String token) {
        // 仅做参数传递
        return learnService.recordLearning(learnDTO, token);
    }


    @Operation(summary = "查询学习记录", description = "根据时间范围查询学习记录，可分页查询")
    @GetMapping("/select")
    public Result<PageInfo<Learn>> getUserRecords(
            @RequestHeader("Authorization") String token,
            @RequestParam(required = false) LocalDateTime startTime,
            @RequestParam(required = false) LocalDateTime endTime,
            @RequestParam(required = false, defaultValue = "day") String range,
            // 新增分页参数
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize
    ) {
        Long userId = jwtUtil.parseToken(token.substring(7)).get("id", Long.class);

        // 优先处理明确的时间范围
        if (startTime != null && endTime != null) {
            if (startTime.isAfter(endTime)) {
                return Result.error(-1, "开始时间不能晚于结束时间");
            }
            PageHelper.startPage(pageNum, pageSize);
            List<Learn> list = learnService.selectByTimeRange(userId, startTime, endTime);
            return Result.success(new PageInfo<>(list));
        }

        // 处理range参数
        switch (range.toLowerCase()) {
            case "week":
                PageHelper.startPage(pageNum, pageSize);
                List<Learn> weekList = learnService.selectWeekRecords(userId);
                return Result.success(new PageInfo<>(weekList));
            case "day":
            default:
                PageHelper.startPage(pageNum, pageSize);
                List<Learn> dayList = learnService.selectTodayRecords(userId);
                return Result.success(new PageInfo<>(dayList));
        }
    }

//    @GetMapping("/records")
//    public Result<List<Learn>> getUserRecords(
//            @RequestHeader("Authorization") String token,
//            @RequestParam(required = false) LocalDateTime startTime,
//            @RequestParam(required = false) LocalDateTime endTime,
//            @RequestParam(required = false, defaultValue = "day") String range
//    ) {
//        // 解析token获取用户ID
//        Long userId = jwtUtil.parseToken(token.substring(7)).get("id", Long.class);
//
//        // 优先处理明确的时间范围
//        if (startTime != null && endTime != null) {
//            if (startTime.isAfter(endTime)) {
//                return Result.error(-1, "开始时间不能晚于结束时间");
//            }
//            return learnService.selectByTimeRange(userId, startTime, endTime);
//        }
//
//        // 处理range参数
//        switch (range.toLowerCase()) {
//            case "week":
//                return learnService.selectWeekRecords(userId);
//            case "day":
//            default:
//                return learnService.selectTodayRecords(userId);
//        }
//    }
}
